<?php

namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);

use app\admin\Controller;
use think\Db;
use think\Config;
use think\Request;

class Driver extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $data = input();
        if($this::$isdelete==1){
            $map['d.isdelete']=1;
        }else{

            $map['d.isdelete']=0;
        }
        if ($data['user_id']) {
            $map['d.user_id'] = $data['user_id'];
        }
        if ($data['plate_number']) {
            $map['d.plate_number'] = array('like',"%{$data['plate_number']}%");
        }
        if ($data['weixin_no']) {
            $map['d.weixin_no'] = array('like',"%{$data['weixin_no']}%");
        }
        if ($data['realname']) {
            $map['u.realname'] = array('like',"%{$data['realname']}%");
        }

        if ($data['mobile']) {
            $map['u.account'] = array('like',"%{$data['mobile']}%");
        }
        if ($data['seats']===0||$data['seats']) {
            $map['d.seats'] = $data['seats'];
        }
        if ($data['nationality']){
            $map['d.nationality'] = ['like','%'.$data['nationality'].'%'];
        }
        if ($data['isagree']===0||$data['isagree']) {
            $map['d.isagree'] = $data['isagree'];
        }
        if($data['tag']){
            $map['d.tag']=array('like',"%{$data['tag']}%");
        }
        if($data['driver_rank_id'] == 5){
            $map['d.driver_rank_id']=0;
        }elseif ($data['driver_rank_id']){
            $map['d.driver_rank_id']=$data['driver_rank_id'];
        }
        if($data['driver_type']){
            $map['d.driver_type']=$data['driver_type'];
        }

        //增加车型搜索
        if ($data['car_model']) {
            $map['d.car_model'] = array('like',"%{$data['car_model']}%");
        }

        // 增加车牌搜索
        if ($data['car_brand']){
            $map['d.car_brand'] = ['like',"%{$data['car_brand']}%"];
        }

        if ($data['car_type']){
            $map['d.car_type'] = ['like','%'.$data['car_type'].'%'];
        }



        $listRows = $this->request->param('numPerPage') ?: Config::get("paginate.list_rows");
        $list = Db::name('driver')
            ->alias("d")
            ->join("tp_admin_user u", "u.id=d.user_id", "left")
            ->join("tp_driver_rank r", "r.id=d.driver_rank_id", "left")
            ->where($map)
            ->order("d.id desc")
            ->field("d.*,u.account,u.realname,r.name as r_name")
            ->paginate($listRows, false, ['query' => $this->request->get()]);
//        dump($list);
        // 模板赋值显示
        $map_tags['isdelete']=0;
        $map_tags['status']=1;
        $driver_tags=Db::name('driver_tag')
            ->where($map_tags)
            ->select();
        //查询车型
        Db::name('car_type')->select();
        $this->view->assign('driver_tags',$driver_tags);
        $ranks=Db::name('driver_rank')->select();
        $driver_type = Db::name('driver_type')->select();
        $this->view->assign('ranks',$ranks);
        $this->view->assign('driver_type',$driver_type);
        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        return $this->view->fetch();
    }

    /**
     * 已通过司机
     * @return mixed
     */
    public function inagree()
    {
        $data = input();
        if ($data['user_id']) {
            $map['d.user_id'] = $data['user_id'];
        }
        if ($data['nationality']){
            $map['d.nationality'] = ['like','%'.$data['nationality'].'%'];
        }
        if ($data['realname']) {
            $map['u.realname'] = array('like',"%{$data['realname']}%");
        }
        if ($data['plate_number']) {
            $map['d.plate_number'] = array('like',"%{$data['plate_number']}%");
        }
        if ($data['car_model']) {
            $map['d.car_model'] = array('like',"%{$data['car_model']}%");
        }
        if ($data['weixin_no']) {
            $map['d.weixin_no'] = array('like',"%{$data['weixin_no']}%");
        }
        // 增加车牌搜索
        if ($data['car_brand']){
            $map['d.car_brand'] = ['like',"%{$data['car_brand']}%"];
        }
        if ($data['mobile']) {
            $map['u.account'] = array('like',"%{$data['mobile']}%");
        }
        if ($data['seats']===0||$data['seats']) {
            $map['d.seats'] = $data['seats'];
        }
        $map['d.isagree'] =true;
        if($data['tag']){
            $map['d.tag']=array('like',"%{$data['tag']}%");
        }
        if($data['driver_rank_id']){
            if ($data['driver_rank_id'] == 5){
                $map['d.driver_rank_id']=0;
            }else{
                $map['d.driver_rank_id']=$data['driver_rank_id'];
            }
        }

        if ($data['car_type']){
            $map['d.car_type'] = ['like','%'.$data['car_type'].'%'];
        }

        if ($data['car_brand']){
            $map['d.car_brand'] = ['like','%'.$data['car_brand'].'%'];
        }

        if ($data['car_model']) {
            $map['d.car_model'] = ['like','%'.$data['car_model'].'%'];
        }
        $listRows = $this->request->param('numPerPage') ?: Config::get("paginate.list_rows");
        $list = Db::name('driver')
            ->alias("d")
            ->join("tp_admin_user u", "u.id=d.user_id", "left")
            ->join("tp_driver_rank r", "r.id=d.driver_rank_id", "left")
            ->where($map)
            ->order("d.id desc")
            ->field("d.*,u.account,u.realname,r.name as r_name")
            ->paginate($listRows, false, ['query' => $this->request->get()]);
//        dump($list);
        // 模板赋值显示
        $map_tags['isdelete']=0;
        $map_tags['status']=1;
        $driver_tags=Db::name('driver_tag')
            ->where($map_tags)
            ->select();
        $this->view->assign('driver_tags',$driver_tags);
        $ranks=Db::name('driver_rank')->select();
        $this->view->assign('ranks',$ranks);
        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        return $this->view->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data = input();
            if($data['head_img']){
                $data['head_img']=get_default_img($data['head_img'][0],true);
            }
            if($data['car_front']){
                $data['car_front']=get_default_img($data['car_front'][0],true);
            }
            if($data['car_back']){
                $data['car_back']=get_default_img($data['car_back'][0],true);
            }
            if($data['in_car']){
                $data['in_car']=get_default_img($data['in_car'][0],true);
            }
            if($data['trunk_photo']){
                $data['trunk_photo']=get_default_img($data['trunk_photo'][0],true);
            }

            if($data['driving_permit_img']){
                $data['driving_permit_img']=get_default_img($data['driving_permit_img'][0],true);
            }
            if($data['insurance']){
                $data['insurance']=get_default_img($data['insurance'][0],true);
            }
            if($data['id_card_front_img']){
                $data['id_card_front_img']=get_default_img($data['id_card_front_img'][0],true);
            }
            if($data['id_card_back_img']){
                $data['id_card_back_img']=get_default_img($data['id_card_back_img'][0],true);
            }
            if($data['take_id_card_img']){
                $data['take_id_card_img']=get_default_img($data['take_id_card_img'][0],true);
            }
            if($data['driver_license_img']){
                $data['driver_license_img']=get_default_img($data['driver_license_img'][0],true);
            }
            if($data['other_certificate']){
                $data['other_certificate']=get_default_img($data['other_certificate'][0],true);
            }
            if($data['other_certificate1']){
                $data['other_certificate1']=get_default_img($data['other_certificate1'][0],true);
            }
            if($data['other_certificate2']){
                $data['other_certificate2']=get_default_img($data['other_certificate2'][0],true);
            }
            if ($data['tag']){
                $data['tag'] = $data['tag'][0];
            }
            //车辆类型
            if ($data['car_type_id']){
                $data['arctic_id'] = $data['car_type_id'];
                $data['car_type'] = Db::name('arctic')->where('id',$data['car_type_id'])->value('name');
            }
            // 车辆品牌
            if ($data['car_brand_id']){
                $data['car_brand'] = Db::name('car_brand')->where('id',$data['car_brand_id'])->value('brand_name');
            }
            // 汽车型号
            if ($data['car_model_id']){
                $data['car_model'] = Db::name('car_model')->where('id',$data['car_model_id'])->value('car_model');
            }
            Db::name('driver')->strict(false)->insert($data);
            return ajax_return_adv("添加司机");
        } else {
            $nation = [
                ['id'=>1,'nationality'=>'阿昌族'],
                ['id'=>2,'nationality'=>'鄂温克族'],
                ['id'=>3,'nationality'=>'傈僳族'],
                ['id'=>4,'nationality'=>'水族'],
                ['id'=>5,'nationality'=>'白族'],
                ['id'=>6,'nationality'=>'高山族'],
                ['id'=>7,'nationality'=>'珞巴族'],
                ['id'=>8,'nationality'=>'塔吉克族'],
                ['id'=>9,'nationality'=>'保安族'],
                ['id'=>10,'nationality'=>'仡佬族'],
                ['id'=>11,'nationality'=>'满族'],
                ['id'=>12,'nationality'=>'塔塔尔族'],
                ['id'=>13,'nationality'=>'布朗族'],
                ['id'=>14,'nationality'=>'哈尼族'],
                ['id'=>15,'nationality'=>'毛南族'],
                ['id'=>16,'nationality'=>'土家族'],
                ['id'=>17,'nationality'=>'布依族'],
                ['id'=>18,'nationality'=>'哈萨克族'],
                ['id'=>19,'nationality'=>'门巴族'],
                ['id'=>20,'nationality'=>'土族'],
                ['id'=>21,'nationality'=>'朝鲜族'],
                ['id'=>22,'nationality'=>'汉族'],
                ['id'=>23,'nationality'=>'蒙古族'],
                ['id'=>24,'nationality'=>'佤族'],
                ['id'=>25,'nationality'=>'达斡尔族'],
                ['id'=>26,'nationality'=>'赫哲族'],
                ['id'=>27,'nationality'=>'苗族'],
                ['id'=>28,'nationality'=>'维吾尔族'],
                ['id'=>29,'nationality'=>'傣族'],
                ['id'=>30,'nationality'=>'回族'],
                ['id'=>31,'nationality'=>'仫佬族'],
                ['id'=>32,'nationality'=>'乌孜别克族'],
                ['id'=>33,'nationality'=>'德昂族'],
                ['id'=>34,'nationality'=>'基诺族'],
                ['id'=>35,'nationality'=>'纳西族'],
                ['id'=>36,'nationality'=>'锡伯族'],
                ['id'=>37,'nationality'=>'东乡族'],
                ['id'=>38,'nationality'=>'京族'],
                ['id'=>39,'nationality'=>'怒族'],
                ['id'=>40,'nationality'=>'瑶族'],
                ['id'=>41,'nationality'=>'侗族'],
                ['id'=>42,'nationality'=>'景颇族'],
                ['id'=>43,'nationality'=>'普米族'],
                ['id'=>44,'nationality'=>'彝族'],
                ['id'=>45,'nationality'=>'独龙族'],
                ['id'=>46,'nationality'=>'柯尔克孜族'],
                ['id'=>47,'nationality'=>'羌族'],
                ['id'=>48,'nationality'=>'裕固族'],
                ['id'=>49,'nationality'=>'俄罗斯族'],
                ['id'=>50,'nationality'=>'拉祜族'],
                ['id'=>51,'nationality'=>'撒拉族'],
                ['id'=>52,'nationality'=>'藏族'],
                ['id'=>53,'nationality'=>'鄂伦春族'],
                ['id'=>54,'nationality'=>'黎族'],
                ['id'=>55,'nationality'=>'畲族'],
                ['id'=>56,'nationality'=>'壮族'],
            ];
            $users = Db::name('admin_user')->select();
            $seat = [];
            for ($i = 1; $i < 81; $i++) {
                $seat[] = [
                    'name' => "座位数为:{$i}",
                    'value' => $i,
                ];
            }
            //查找所有标签
            $driverTags = Db::name('driver_tag')->field('id,chinese')->select();
            // 获取所有车类型
            $arctic = Db::name('arctic')->where('isdelete',0)->field('id,name')->select();
            // 查询所有司机等级
            $driverRank = Db::name('driver_rank')->select();
            $this->view->assign('arctic',$arctic);
            $this->view->assign('tag',$driverTags);
            $this->view->assign('users', $users);
            $this->view->assign('seat', $seat);
            $this->view->assign('nation',$nation);
            $this->view->assign('driverRank',$driverRank);
            return $this->view->fetch('add');
        }
    }

    /**
     * 获取车牌列表
     */
    public function get_car_brand()
    {
        $data = input();
        if (!$data['id']){
            return ajax_return_error('缺少必要的参数');
        }
        $car_brand_list = Db::name('car_brand')->where('arctic_id',$data['id'])->order('id','asc')->select();
        if($car_brand_list) {
            // 查找排在第一品牌的汽车型号
            $car_model_list = Db::name('car_model')->where('car_brand_id',$car_brand_list[0]['id'])->select();
            $msg['car_brand_list'] = $car_brand_list;
            $msg['car_model_list'] = $car_model_list;
            return ajax_return_adv('获取数据成功','','','','',$msg);
        }else{
            return ajax_return_error('该车型不存在数据');
        }
    }

    /**
     * 获取汽车型号列表
     */
    public function get_car_model()
    {
        $data = input();
        if (!$data['id']){
            return ajax_return_error('缺少必要的参数');
        }
        $car_model_list = Db::name('car_model')->where('car_brand_id',$data['id'])->select();
        if($car_model_list) {
            return ajax_return_adv('获取数据成功','','','','',$car_model_list);
        }else{
            return ajax_return_error('该车型不存在数据');
        }
    }

    /**
     * 审核详情
     * @return mixed
     */
    public function check_info()
    {
        if ($this->request->isAjax()) {
            $data = input();
            if($data['head_img']){
                $data['head_img']=get_default_img($data['head_img'][0],true);
            }
            if($data['car_front']){
                $data['car_front']=get_default_img($data['car_front'][0],true);
            }
            if($data['car_back']){
                $data['car_back']=get_default_img($data['car_back'][0],true);
            }
            if($data['in_car']){
                $data['in_car']=get_default_img($data['in_car'][0],true);
            }
            if($data['trunk_photo']){
                $data['trunk_photo']=get_default_img($data['trunk_photo'][0],true);
            }

            if($data['driving_permit_img']){
                $data['driving_permit_img']=get_default_img($data['driving_permit_img'][0],true);
            }
            if($data['insurance']){
                $data['insurance']=get_default_img($data['insurance'][0],true);
            }
            if($data['id_card_front_img']){
                $data['id_card_front_img']=get_default_img($data['id_card_front_img'][0],true);
            }
            if($data['id_card_back_img']){
                $data['id_card_back_img']=get_default_img($data['id_card_back_img'][0],true);
            }
            if($data['take_id_card_img']){
                $data['take_id_card_img']=get_default_img($data['take_id_card_img'][0],true);
            }
            if($data['driver_license_img']){
                $data['driver_license_img']=get_default_img($data['driver_license_img'][0],true);
            }
            if($data['other_certificate']){
                $data['other_certificate']=get_default_img($data['other_certificate'][0],true);
            }
            if($data['other_certificate1']){
                $data['other_certificate1']=get_default_img($data['other_certificate1'][0],true);
            }
            if($data['other_certificate2']){
                $data['other_certificate2']=get_default_img($data['other_certificate2'][0],true);
            }
            Db::name('driver')->where('id', $data['id'])->update($data);
            return ajax_return_adv("修改通过");
        } else {
            $data = input();
            $vo = Db::name('driver')
                ->alias('d')
                ->join("tp_admin_user u","u.id=d.user_id","left")
                ->where('d.id', $data['id'])
                ->field("d.*,u.realname,u.account")
                ->find();

            $this->view->assign('vo', $vo);
            return $this->view->fetch();
        }
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = input();
            if($data['head_img']){
                $data['head_img']=get_default_img($data['head_img'][0],true);
            }
            if($data['car_front']){
                $data['car_front']=get_default_img($data['car_front'][0],true);
            }
            if($data['car_back']){
                $data['car_back']=get_default_img($data['car_back'][0],true);
            }
            if($data['in_car']){
                $data['in_car']=get_default_img($data['in_car'][0],true);
            }
            if($data['trunk_photo']){
                $data['trunk_photo']=get_default_img($data['trunk_photo'][0],true);
            }

            if($data['driving_permit_img']){
                $data['driving_permit_img']=get_default_img($data['driving_permit_img'][0],true);
            }
            if($data['insurance']){
                $data['insurance']=get_default_img($data['insurance'][0],true);
            }
            if($data['id_card_front_img']){
                $data['id_card_front_img']=get_default_img($data['id_card_front_img'][0],true);
            }
            if($data['id_card_back_img']){
                $data['id_card_back_img']=get_default_img($data['id_card_back_img'][0],true);
            }
            if($data['take_id_card_img']){
                $data['take_id_card_img']=get_default_img($data['take_id_card_img'][0],true);
            }
            if($data['driver_license_img']){
                $data['driver_license_img']=get_default_img($data['driver_license_img'][0],true);
            }
            if($data['other_certificate']){
                $data['other_certificate']=get_default_img($data['other_certificate'][0],true);
            }
            if($data['other_certificate1']){
                $data['other_certificate1']=get_default_img($data['other_certificate1'][0],true);
            }
            if($data['other_certificate2']){
                $data['other_certificate2']=get_default_img($data['other_certificate2'][0],true);
            }
            if ($data['tag']){
                //先删除司机标签
                Db::name('driver')->where('id', $data['id'])->update(['tag'=>'']);
                $data['tag'] = $data['tag'][0];
            }

            //车辆类型
            if ($data['car_type_id']){
                $data['arctic_id'] = $data['car_type_id'];
                $data['car_type'] = Db::name('arctic')->where('id',$data['car_type_id'])->value('name');
            }
            // 车辆品牌
            if ($data['car_brand_id']){
                $data['car_brand'] = Db::name('car_brand')->where('id',$data['car_brand_id'])->value('brand_name');
            }
            // 汽车型号
            if ($data['car_model_id']){
                $data['car_model'] = Db::name('car_model')->where('id',$data['car_model_id'])->value('car_model');
            }

            Db::name('driver')->where('id', $data['id'])->strict(false)->update($data);
            return ajax_return_adv("修改通过");
        } else {
            $data = input();
            $vo = Db::name('driver')->where('id', $data['id'])->find();
            if (!strrpos($vo['nationality'],'族')){
                $vo['nationality'] = $vo['nationality'].'族';
            }
            $nation = [
                ['id'=>1,'nationality'=>'阿昌族'],
                ['id'=>2,'nationality'=>'鄂温克族'],
                ['id'=>3,'nationality'=>'傈僳族'],
                ['id'=>4,'nationality'=>'水族'],
                ['id'=>5,'nationality'=>'白族'],
                ['id'=>6,'nationality'=>'高山族'],
                ['id'=>7,'nationality'=>'珞巴族'],
                ['id'=>8,'nationality'=>'塔吉克族'],
                ['id'=>9,'nationality'=>'保安族'],
                ['id'=>10,'nationality'=>'仡佬族'],
                ['id'=>11,'nationality'=>'满族'],
                ['id'=>12,'nationality'=>'塔塔尔族'],
                ['id'=>13,'nationality'=>'布朗族'],
                ['id'=>14,'nationality'=>'哈尼族'],
                ['id'=>15,'nationality'=>'毛南族'],
                ['id'=>16,'nationality'=>'土家族'],
                ['id'=>17,'nationality'=>'布依族'],
                ['id'=>18,'nationality'=>'哈萨克族'],
                ['id'=>19,'nationality'=>'门巴族'],
                ['id'=>20,'nationality'=>'土族'],
                ['id'=>21,'nationality'=>'朝鲜族'],
                ['id'=>22,'nationality'=>'汉族'],
                ['id'=>23,'nationality'=>'蒙古族'],
                ['id'=>24,'nationality'=>'佤族'],
                ['id'=>25,'nationality'=>'达斡尔族'],
                ['id'=>26,'nationality'=>'赫哲族'],
                ['id'=>27,'nationality'=>'苗族'],
                ['id'=>28,'nationality'=>'维吾尔族'],
                ['id'=>29,'nationality'=>'傣族'],
                ['id'=>30,'nationality'=>'回族'],
                ['id'=>31,'nationality'=>'仫佬族'],
                ['id'=>32,'nationality'=>'乌孜别克族'],
                ['id'=>33,'nationality'=>'德昂族'],
                ['id'=>34,'nationality'=>'基诺族'],
                ['id'=>35,'nationality'=>'纳西族'],
                ['id'=>36,'nationality'=>'锡伯族'],
                ['id'=>37,'nationality'=>'东乡族'],
                ['id'=>38,'nationality'=>'京族'],
                ['id'=>39,'nationality'=>'怒族'],
                ['id'=>40,'nationality'=>'瑶族'],
                ['id'=>41,'nationality'=>'侗族'],
                ['id'=>42,'nationality'=>'景颇族'],
                ['id'=>43,'nationality'=>'普米族'],
                ['id'=>44,'nationality'=>'彝族'],
                ['id'=>45,'nationality'=>'独龙族'],
                ['id'=>46,'nationality'=>'柯尔克孜族'],
                ['id'=>47,'nationality'=>'羌族'],
                ['id'=>48,'nationality'=>'裕固族'],
                ['id'=>49,'nationality'=>'俄罗斯族'],
                ['id'=>50,'nationality'=>'拉祜族'],
                ['id'=>51,'nationality'=>'撒拉族'],
                ['id'=>52,'nationality'=>'藏族'],
                ['id'=>53,'nationality'=>'鄂伦春族'],
                ['id'=>54,'nationality'=>'黎族'],
                ['id'=>55,'nationality'=>'畲族'],
                ['id'=>56,'nationality'=>'壮族'],
            ];
            $users = Db::name('admin_user')->select();
            //查找所有标签
            $driverTags = Db::name('driver_tag')->field('id,chinese')->select();
            // 查询车类型
            $arctic = Db::name('arctic')->select();
            //查询车辆品牌
            $carBrandList = Db::name('car_brand')->where('arctic_id',$vo['arctic_id'])->select();
            // 查询汽车型号
            $carModel = Db::name('car_model')->where('car_brand_id',$vo['car_brand_id'])->select();
            // 查询所有司机等级
            $driverRank = Db::name('driver_rank')->select();
            $this->view->assign('tag',$driverTags);
            $this->view->assign('users', $users);
            $this->view->assign('arctic', $arctic);
            $this->view->assign('car_brand_list', $carBrandList);
            $this->view->assign('car_model', $carModel);
            $this->view->assign('vo', $vo);
            $this->view->assign('nation',$nation);
            $this->view->assign('driverRank',$driverRank);
            return $this->view->fetch('edit');
        }
    }

    /**
     * 申请
     * @return mixed
     */
    public function apply()
    {
        $data = input();
        if ($data['user_id']) {
            $driver['check_status'] = 'applying';
            $driver['isagree'] = false;
            $re = Db::name('driver')->where('user_id', $data['user_id'])->update($driver);
            if ($re !== false) {
                //发送消息
                return ajax_return_adv("申请中");
            }
        } else {
            return ajax_return_adv_error("提交失败");
        }
    }

    /**
     * 审核通过
     * @return mixed
     */
    public function agree()
    {
        $data = input();
        if ($data['user_id']) {
            $driver['check_status'] = 'checked';
            $driver['ischeck'] = true;
            $driver['isagree'] = true;
            $re = Db::name('driver')->where('user_id', $data['user_id'])->update($driver);
            if ($re !== false) {
                //发送消息
                set_info($data['user_id'],"司导审核结果",'司导审核结果通过','driver_check_result');
                return ajax_return_adv("审核通过");
            }
        } else {
            return ajax_return_adv_error("提交失败");
        }
    }

    /**
     * 审核
     * @return mixed
     */
    public function check()
    {
        $data = input();
        if ($data['user_id']) {
            $map['d.user_id'] = $data['user_id'];
        }
        if ($data['plate_number']) {
            $map['d.plate_number'] = array('like',"%{$data['plate_number']}%");
        }
        if ($data['weixin_no']) {
            $map['d.weixin_no'] = array('like',"%{$data['weixin_no']}%");
        }

        if ($data['mobile']) {
            $map['u.account'] = array('like',"%{$data['mobile']}%");
        }
        if ($data['seats']===0||$data['seats']) {
            $map['d.seats'] = $data['seats'];
        }
        if($data['tag']){
            $map['d.tag']=array('like',"%{$data['tag']}%");
        }
        $map['d.isagree']=false;

        if($data['driver_rank_id']){
            $map['d.driver_rank_id']=$data['driver_rank_id'];
        }

        $listRows = $this->request->param('numPerPage') ?: Config::get("paginate.list_rows");
        $list = Db::name('driver')
            ->alias("d")
            ->join("tp_admin_user u", "u.id=d.user_id", "left")
            ->join("tp_driver_rank r", "r.id=d.driver_rank_id", "left")
            ->where($map)
            ->order("d.id desc")
            ->field("d.*,u.account,u.realname,r.name as r_name")
            ->paginate($listRows, false, ['query' => $this->request->get()]);
//        dump($list);
        // 模板赋值显示
        $map_tags['isdelete']=0;
        $map_tags['status']=1;
        $driver_tags=Db::name('driver_tag')
            ->where($map_tags)
            ->select();
        $this->view->assign('driver_tags',$driver_tags);
        $ranks=Db::name('driver_rank')->select();
        $this->view->assign('ranks',$ranks);
        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        return $this->view->fetch();
    }

    /**
     * 审核不通过
     * @return mixed
     */
    public function reject()
    {
        $data = input();
        if ($data['user_id']) {
            $driver['check_status'] = 'checked';
            $driver['ischeck'] = true;
            $driver['isagree'] = false;
            $re = Db::name('driver')->where('user_id', $data['user_id'])->update($driver);
            if ($re !== false) {
                set_info($data['user_id'],"司导审核结果",'司导审核结果不通过','driver_check_result');
                return ajax_return_adv("不通过");
            }
        } else {
            return ajax_return_adv_error("提交失败");
        }
    }

    /**
     * 永久删除
     */
    public function deleteForever()
    {
        $ids = $this->request->param('id');
        $where['id'] = ["in", $ids];
        $user_ids=Db::name('driver')->where($where)->column('user_id');
        Db::name('driver')->where($where)->delete();
        $del_map['id'] =["in", $user_ids];
        Db::name('admin_user')->where($del_map)->delete();
        return ajax_return_adv("删除成功");
    }

    /**
     * 清空回收站
     */
    public function clear()
    {

        $where['isdelete'] = 1;
        $user_ids=Db::name('driver')->where($where)->column('user_id');
        Db::name('driver')->where($where)->delete();
        $del_map['id'] =["in", $user_ids];
        Db::name('admin_user')->where($del_map)->delete();

        return ajax_return_adv("清空回收站成功");
    }

    public function tag()
    {
        $term=input('term');
        $tags=Db::name('driver_tag')
            ->where('chinese','like',"%{$term}%")
            ->field('id,chinese as label, chinese as value')
            ->select();
        return json($tags);
    }

    public function details(Request $request)
    {
        $data = input();
        $users = Db::name('admin_user')->where('id',$data['id'])->find();
        if ($users['id']){
            $users['user_id'] = $users['id'];
            unset($users['id']);
        }
        if (!$users){
            return ajax_return_adv_error('不存在该司机的用户信息',1);
        }
        $vo = Db::name('driver')->where('user_id', $users['user_id'])->find();
        if (!$vo){
            return ajax_return_adv_error('不存在该司机信息');
        }
        //查询司机等级
        $driverRank = Db::name('driver_rank')->where('id',$users['driver_rank_id'])->find();
        if ($driverRank){
            $vo['driver_rank_id'] = $driverRank['name'];
        }else{
            $vo['driver_rank_id'] = '新手';
        }
        $this->view->assign('users', $users);
        $this->view->assign('vo', $vo);
        return $this->view->fetch('details');
    }

    /**
     * 添加司机标签
     */

    public function add_tag(Request $request)
    {
        if ($request->isPost()){
            $data = $request->post();
            if ($data['id']){
                Db::name('driver')->where('id',$data['driver_id'])->update(['tag'=>'']);
                $tag = rtrim(implode(',',$data['id']),',');
            }else{
                $tag = '';
            }
            //查询司机的
            $update = Db::name('driver')->where('id',$data['driver_id'])->update(['tag'=>$tag]);
            if ($update){
                return ajax_return_adv('操作成功');
            }else{
                return ajax_return_adv_error('操作失败');
            }
        }else{
            $driver_id = input('id');
            $driverRankTag = Db::name('driver')->where('id',$driver_id)->value('tag');
            $driverRankTag = explode(',',$driverRankTag);
            $driverDrank = Db::name('driver_tag')->where('isdelete',0)->field('id,chinese')->select();
            return view('add_tag',compact('driverDrank','driver_id','driverRankTag'));
        }
    }

    /**
     * 添加司机银行卡列表
     */
    public function bank_card_info(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            $id = $postData['id'];
            if ($id){
                $up = Db::name('bank_card')->where('id',$id)->delete();
                if ($up){
                    return ajax_return_adv('操作成功');
                }else{
                    return ajax_return_adv_error('操作失败');
                }
            }else{
                return ajax_return_adv_error('缺少参数');
            }
        }else{
            $data = input();
            if (!$data['id']){
                return ajax_return_adv_error('缺少必要的参数!');
            }
            $driverInfo = Db::name('driver')->where('id',$data['id'])->find();
            if ($driverInfo){
                $driverBrankCard = Db::name('bank_card')->where('user_id',$driverInfo['user_id'])->where('isdelete',0)->select();
                $driverUserName = Db::name('admin_user')->where('id',$driverInfo['user_id'])->value('realname');
                return view('',compact('driverBrankCard','driverUserName'));
        }else{
                return ajax_return_adv_error('不存在该用户信息!');
            }
        }
    }

    /**
     * 司机类型列表
     */
    public function driver_type(){
        $data = input();

        $map = [];
        if($data['name']){
            $map['name'] = array('like',"%{$data['name']}%");
        }

        if(is_numeric($data['from'])){
            $map['from'] = array('=',$data['from']);
        }

        $listRows = $this->request->param('numPerPage') ?: Config::get("paginate.list_rows");

        $list = Db::name('driver_type')
            ->where($map)
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $this->view->assign('list', $list);
        return $this->view->fetch();
    }

    /**
     * 添加司机类型
     */
    public function add_driver_type(){
        $data = input();

        if($data['from'] == ''){
            return ajax_return_adv_error('请选择司机类型所属！');
        }

        if(empty($data['name'])){
            return ajax_return_adv_error('请填写司机名称！');
        }

        $ck = Db::name('driver_type')->where('name',$data['name'])->find();
        if($ck){
            return ajax_return_adv_error('该司机名称已存在！');
        }

        $res = Db::name('driver_type')->strict(false)->insert(array('name'=>$data['name'],'from'=>$data['from'],'create_time'=>time()));

        if($res){
            return ajax_return_adv('添加成功');
        }else{
            return ajax_return_adv_error('添加失败');
        }
    }

    /**
     * 修改司机类型
     */
    public function edit_driver_type(){
        $data = input();

        if(!$data['id']){
            return ajax_return_adv_error('缺少参数id！');
        }

        if(empty($data['name'])){
            return ajax_return_adv_error('请填写司机名称！');
        }

        if($data['from'] == ''){
            return ajax_return_adv_error('请选择司机类型所属！');
        }

        $ck = Db::name('driver_type')->where('name',$data['name'])->find();

        if($ck && $ck['id'] != $data['id']){
            return ajax_return_adv_error('该司机名称已存在！');
        }

        $is_use = Db::name('driver')->where('driver_type',$data['id'])->find();

        if($is_use){
            return ajax_return_adv_error('已有司机使用此司机类型不可进行修改！');
        }

        $res = Db::name('driver_type')->strict(false)->where('id',$data['id'])->update(array('name'=>$data['name'],'from'=>$data['from']));

        if($res){
            return ajax_return_adv('修改成功');
        }else{
            return ajax_return_adv_error('修改失败');
        }
    }

    /**
     * 删除司机类型
     */
    public function del_driver_type(){
        $data = input();

        if(!$data['id']){
            return ajax_return_adv_error('缺少参数id！');
        }

        $ck = Db::name('driver_type')->where('id',$data['id'])->find();
        if(!$ck){
            return ajax_return_adv_error('不存在该记录或已被删除！');
        }

        $is_use = Db::name('driver')->where('driver_type',$data['id'])->find();

        if($is_use){
            return ajax_return_adv_error('已有司机使用此司机类型不可进行删除！');
        }

        $res = Db::name('driver_type')->where('id',$data['id'])->delete();

        if($res){
            return ajax_return_adv('删除成功');
        }else{
            return ajax_return_adv_error('删除失败');
        }
    }
}
